package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.zg.autoflow.service.AutoflowStepService;
import com.zg.autoflow.vo.AutoflowStepAddVo;
import com.zg.autoflow.req.AutoflowStepBatchReq;
import com.zg.autoflow.req.AutoflowStepUpdateReq;
import com.zg.common.core.A;
import com.zg.common.core.dao.auth.AuthOrgDao;
import com.zg.common.core.dao.auth.AuthUser;
import com.zg.common.core.dao.auth.AuthUserDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepPersonDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowStep;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepPerson;
import com.zg.common.core.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class AutoflowStepServiceImpl implements AutoflowStepService {

    private final AutoflowStepDao autoflowStepDao;
    private final AutoflowStepPersonDao autoflowStepPersonDao;
    private final AuthUserDao authUserDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowStep add(AutoflowStepAddVo param) {
        //创建对象
        AutoflowStep obj = new AutoflowStep();
        //属性处理
        BeanUtil.copyProperties(param, obj);
        StringBuffer strBuff = new StringBuffer();
        obj.setSearchStr(strBuff.toString());
        //保存对象
        autoflowStepDao.save(obj);
        List<Long> personList = param.getPersonList();
        personList.forEach(person->{
            AutoflowStepPerson autoflowStepPerson = new AutoflowStepPerson();
            autoflowStepPerson.setStepHandler(person);
            autoflowStepPerson.setStepId(obj.getId());
            AuthUser byId = authUserDao.getById(person);
            if (ObjectUtils.isNotEmpty(byId)){
                autoflowStepPerson.setStepHandlerOrg(byId.getOrgId()+"");
            }else {
                BusinessException.happen("环节处理人不存在请检查");
            }
            autoflowStepPersonDao.save(autoflowStepPerson);
        });
        return obj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestParam("ids") List<Long> idList) {
        autoflowStepDao.removeBatchByIds(idList);
    }

    void checkUpdate(AutoflowStepUpdateReq param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowStep update(AutoflowStepUpdateReq param) {
        //查询对象
        AutoflowStep oldObj = autoflowStepDao.getById(param.getId());
        A.isInDatabase(oldObj, "要更新的对象");
        //校验逻辑
        checkUpdate(param);
        //属性处理
        BeanUtil.copyProperties(param, oldObj);
        StringBuffer strBuff = new StringBuffer();
        oldObj.setSearchStr(strBuff.toString());
        //更新对象
        autoflowStepDao.updateById(oldObj);
        return oldObj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(List<AutoflowStepBatchReq>  param) {
        // 分组逻辑
        List<AutoflowStepBatchReq> withId = param.stream()
                .filter(item -> item.getId() != null)
                .collect(Collectors.toList());
        List<AutoflowStepBatchReq> withoutId = param.stream()
                .filter(item -> item.getId() == null)
                .collect(Collectors.toList());
        //创建批量更新列表
        List<AutoflowStep> updateList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(withId)){
            for (AutoflowStepBatchReq autoflowStepBatchVo : withId) {
                AutoflowStep oldObj = autoflowStepDao.getById(autoflowStepBatchVo.getId());
                if(autoflowStepBatchVo.getIdCk()){
                    oldObj.setId(autoflowStepBatchVo.getId());
                }
                if(autoflowStepBatchVo.getStepNoCk()){
                    oldObj.setStepNo(autoflowStepBatchVo.getStepNo());
                }
                if(autoflowStepBatchVo.getStepNameCk()){
                    oldObj.setStepName(autoflowStepBatchVo.getStepName());
                }
                if(autoflowStepBatchVo.getStepHandlerCk()){
                    oldObj.setStepHandler(autoflowStepBatchVo.getStepHandler());
                }
                if(autoflowStepBatchVo.getStepFormIdCk()){
                    oldObj.setStepFormId(autoflowStepBatchVo.getStepFormId());
                }
                if(autoflowStepBatchVo.getNextStepIdCk()){
                    oldObj.setNextStepId(autoflowStepBatchVo.getNextStepId());
                }
                if(autoflowStepBatchVo.getCreateTimeCk()){
                    oldObj.setCreateTime(autoflowStepBatchVo.getCreateTime());
                }
                if(autoflowStepBatchVo.getUpdateTimeCk()){
                    oldObj.setUpdateTime(autoflowStepBatchVo.getUpdateTime());
                }
                if(autoflowStepBatchVo.getFlowIdCk()){
                    oldObj.setFlowId(autoflowStepBatchVo.getFlowId());
                }
                if(autoflowStepBatchVo.getStepHandlerOrgCk()){
                    oldObj.setStepHandlerOrg(autoflowStepBatchVo.getStepHandlerOrg());
                }
                if(autoflowStepBatchVo.getStepTypeCk()){
                    oldObj.setStepType(autoflowStepBatchVo.getStepType());
                }
                if(autoflowStepBatchVo.getLeftIndexCk()){
                    oldObj.setLeftIndex(autoflowStepBatchVo.getLeftIndex());
                }
                if(autoflowStepBatchVo.getTopIndexCk()){
                    oldObj.setTopIndex(autoflowStepBatchVo.getTopIndex());
                }
                if(autoflowStepBatchVo.getIcoCk()){
                    oldObj.setIco(autoflowStepBatchVo.getIco());
                }
                if(autoflowStepBatchVo.getDesignIdCk()){
                    oldObj.setDesignId(autoflowStepBatchVo.getDesignId());
                }
                if(autoflowStepBatchVo.getStepStateCk()){
                    oldObj.setStepState(autoflowStepBatchVo.getStepState());
                }
                if(autoflowStepBatchVo.getApproverNamesCk()){
                    oldObj.setApproverNames(autoflowStepBatchVo.getApproverNames());
                }
                if(autoflowStepBatchVo.getApproverIdsCk()){
                    oldObj.setApproverIds(autoflowStepBatchVo.getApproverIds());
                }
                if(autoflowStepBatchVo.getStepFormCk()){
                    oldObj.setStepForm(autoflowStepBatchVo.getStepForm());
                }
                if(autoflowStepBatchVo.getIsEditCk()){
                    oldObj.setIsEdit(autoflowStepBatchVo.getIsEdit());
                }
                if(autoflowStepBatchVo.getIsSealCk()){
                    oldObj.setIsSeal(autoflowStepBatchVo.getIsSeal());
                }
                if(autoflowStepBatchVo.getIsReturnCk()){
                    oldObj.setIsReturn(autoflowStepBatchVo.getIsReturn());
                }
                if(autoflowStepBatchVo.getIsReturnedCk()){
                    oldObj.setIsReturned(autoflowStepBatchVo.getIsReturned());
                }
                if(autoflowStepBatchVo.getSeqCk()){
                    oldObj.setSeq(autoflowStepBatchVo.getSeq());
                }
                if(autoflowStepBatchVo.getApproveTypeCk()){
                    oldObj.setApproveType(autoflowStepBatchVo.getApproveType());
                }
                if(autoflowStepBatchVo.getSealDataCk()){
                    oldObj.setSealData(autoflowStepBatchVo.getSealData());
                }
                if(autoflowStepBatchVo.getIsStepEndCk()){
                    oldObj.setIsStepEnd(autoflowStepBatchVo.getIsStepEnd());
                }
                if(autoflowStepBatchVo.getStepFlagCk()){
                    oldObj.setStepFlag(autoflowStepBatchVo.getStepFlag());
                }
                if(autoflowStepBatchVo.getBillStepCk()){
                    oldObj.setBillStep(autoflowStepBatchVo.getBillStep());
                }
                if(autoflowStepBatchVo.getLockStatusCk()){
                    oldObj.setLockStatus(autoflowStepBatchVo.getLockStatus());
                }
                if(autoflowStepBatchVo.getSelectHandlerModeCk()){
                    oldObj.setSelectHandlerMode(autoflowStepBatchVo.getSelectHandlerMode());
                }
                if(autoflowStepBatchVo.getShortMessageCk()){
                    oldObj.setShortMessage(autoflowStepBatchVo.getShortMessage());
                }
                if(autoflowStepBatchVo.getShortMessageTypeCk()){
                    oldObj.setShortMessageType(autoflowStepBatchVo.getShortMessageType());
                }
                if(autoflowStepBatchVo.getStepParameterCk()){
                    oldObj.setStepParameter(autoflowStepBatchVo.getStepParameter());
                }
                if(autoflowStepBatchVo.getOrgTypeCk()){
                    oldObj.setOrgType(autoflowStepBatchVo.getOrgType());
                }
                if(autoflowStepBatchVo.getIsBackApprovalCk()){
                    oldObj.setIsBackApproval(autoflowStepBatchVo.getIsBackApproval());
                }
                if(autoflowStepBatchVo.getFlowStepTypeCk()){
                    oldObj.setFlowStepType(autoflowStepBatchVo.getFlowStepType());
                }
                if(autoflowStepBatchVo.getCanAppCk()){
                    oldObj.setCanApp(autoflowStepBatchVo.getCanApp());
                }
                if(autoflowStepBatchVo.getZtCk()){
                    oldObj.setZt(autoflowStepBatchVo.getZt());
                }
                oldObj.setSearchStr("");
                updateList.add(oldObj);
            }
            autoflowStepDao.updateBatchById(updateList);
        }
        if (CollectionUtil.isNotEmpty(withoutId)){
            List<AutoflowStep> autoflowSteps = BeanUtil.copyToList(withoutId, AutoflowStep.class);
            autoflowStepDao.saveBatch(autoflowSteps);
        }
    }
}
